package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbBrandMapper;
import com.dongdongshop.model.TbBrand;
import com.dongdongshop.model.TbBrandExample;
import com.dongdongshop.page.PageParms;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.vo.BrandVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private TbBrandMapper tbBrandMapper;

    @Override
    public PageResult findBrand(PageParms<BrandVO> page) {
        //取出内容存入对象
        BrandVO brandVO = page.getParm();
        //模糊查询
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(brandVO.getFirstChar())){
            criteria.andFirstCharEqualTo(brandVO.getFirstChar());
        }
        if (StringUtils.isNotBlank(brandVO.getName())){
            criteria.andNameLike("%"+brandVO.getName()+"%");
        }
        //分页工具类
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<TbBrand> list = tbBrandMapper.selectByExample(example);
        PageInfo info = new PageInfo(list);
        //landom表达式 : 将查询到的值复制到VO集合中
        List<BrandVO> brandVOList = list.stream().map(brand -> {
            BrandVO vo = new BrandVO();
            BeanUtils.copyProperties(brand, vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageResult(page.getPageNum(),page.getPageSize(),brandVOList,info.getTotal());
    }


    @Override
    public void addBrandVO(BrandVO brandVO) {
        TbBrand brand = new TbBrand();
        BeanUtils.copyProperties(brandVO, brand);
        tbBrandMapper.insertSelective(brand);
    }

    @Override
    public void deleteBrandVO(Long[] ids) {
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        tbBrandMapper.deleteByExample(example);
    }

    @Override
    public void updateBranVO(BrandVO brandVO) {
        TbBrand brand = new TbBrand();
        BeanUtils.copyProperties(brandVO, brand);
        tbBrandMapper.updateByPrimaryKey(brand);
    }

    @Override
    public BrandVO findBrandById(Long id) {
        TbBrand tbBrand = tbBrandMapper.selectByPrimaryKey(id);
        Function<TbBrand, BrandVO> function = tb -> {
            BrandVO brandVO = new BrandVO();
            BeanUtils.copyProperties(tb,brandVO);
            return brandVO;
        };
        return function.apply(tbBrand);
    }

    @Override
    public List<BrandVO> findBrands(BrandVO vo) {
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(vo.getFirstChar())){
            criteria.andFirstCharEqualTo(vo.getFirstChar());
        }
        if (StringUtils.isNotBlank(vo.getName())){
            criteria.andNameLike("%"+vo.getName()+"%");
        }
        List<TbBrand> list = tbBrandMapper.selectByExample(example);
        List<BrandVO> collect = list.stream().map(brand -> {
            BrandVO brandVO = new BrandVO();
            BeanUtils.copyProperties(brand, brandVO);
            return brandVO;
        }).collect(Collectors.toList());
        List<BrandVO> BrandVO = JSONObject.parseArray(JSONObject.toJSONString(list), BrandVO.class);
        return BrandVO;
    }

    @Override
    public List<TbBrand> getAll() {
        return tbBrandMapper.selectByExample(null);
    }
}
